package com.jingdianjichi.club.oss.controller;

import com.jingdianjichi.club.oss.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author 86156
 * @version 1.0
 * @date 2024-10-25 11:42
 */
@RestController
@RefreshScope
public class FileController {

    @Resource
    private FileService fileService;

    @Value(value = "${storage.service.type}")
    private String storageType;

    @GetMapping("/listBuckets")
    public String listBuckets() throws Exception {
        List<String> listBucket = fileService.listBucket();
        return listBucket.toString();
    }

    @GetMapping("/testNacos")
    public String testNacos() throws Exception {
        return storageType;
    }

    /**
     * 上传文件
     * @return 返回存储对象的url地址
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile file, String bucketName, String objectName){
        objectName = objectName +"/"+file.getOriginalFilename();
        return fileService.uploadFile(file,bucketName,objectName);
    }

}
